package br.uff.gdse.positionevaluation.evaluators;

import java.util.ArrayList;
import java.util.List;

import ru.ispras.sedna.driver.DatabaseManager;
import ru.ispras.sedna.driver.DriverException;
import ru.ispras.sedna.driver.SednaConnection;
import ru.ispras.sedna.driver.SednaSerializedResult;
import ru.ispras.sedna.driver.SednaStatement;
import br.uff.gdse.positionevaluation.queries.XMLQuery;



public class SednaInterface implements IDBInterface {

	@Override
	public List<Object> executeQuery(XMLQuery query){
		
		SednaConnection con = null;  
		List<Object> result = new ArrayList<Object>();
	    try {  
	      /* Get a connection */  
	      con = DatabaseManager.getConnection("localhost",  
	                                          "dblp",  
	                                          "SYSTEM",  
	                                          "MANAGER");  
	      con.begin();  
	 
	      /* Create statement */  
	      SednaStatement st = con.createStatement();  
	 
	      System.out.println(query.getQuery());
	      st.execute(query.getQuery());  
	 
	      SednaSerializedResult pr = st.getSerializedResult();
	      result.add(pr.next());
		    
	      /* Print query results */  
	      //printQueryResults(st);  
	 
	      con.commit();  
	    }  
	    catch(DriverException e) {  
	        e.printStackTrace();  
	    }  
	    finally {  
	      /* Properly close connection */  
	      try { if(con != null) con.close(); }  
	      catch(DriverException e) {  
	        e.printStackTrace();  
	      }  
	    }  
	    
	    return result;
	}
	

	  /* Pretty printing for query results */  
	private static void printQueryResults(SednaStatement st)  
	    throws DriverException {  
	    int count = 1;
	    String item;
	    SednaSerializedResult pr = st.getSerializedResult();
	    while ((item = pr.next()) != null) {  
	      System.out.println(count + " item: " + item);  
	      count++;  
	    }  
	}  

}
